package com.xucan.clonex;

class A {
    int i = 20;

    A() {
    }

    A(int i) {
        this.i = i;
    }
}

class B implements Cloneable {
    private int i = 10;
    private A a = new A(11);

    public Object clone() {
        B b = null;
        try {
            b = (B) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return b;
    }

    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }
}

public class CloneTest {
    public static void main(String[] args) {
        B b1 = new B();
        // 执行浅拷贝
        System.out.println("执行浅拷贝：");
        B cb1 = (B) b1.clone();
        System.out.println(cb1.equals(b1));// false-表示clone的对象与原对象不同
        System.out.println((cb1.getA()).equals(b1.getA()));// true-表示浅拷贝引用对象不变
    }
}